<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-8-10
 * Time: 上午11:25
 */

namespace Utils;


use Data\Account_flow;
use Data\Account_flow_DAO;
use Data\Gonging_entry_order_form_DAO;
use Data\Order_form_DAO;

class EntryOrderForm
{
    public static function entryOrderForm() {
        // 当前时间戳
        $time_now = time();
        // 24小时之前时间戳
        $time = $time_now-24*60*60;

        // 可入帐订单
        $order_form_list = Gonging_entry_order_form_DAO::getCanEntryList($time);
        foreach ($order_form_list as $order_form_record) {
            $order_form_id = $order_form_record[ORDER_FORM_ID];
            $order_form_record = Order_form_DAO::getRecord($order_form_id);
            $restaurant_id = $order_form_record->getRESTAURANT_ID();

            $last_account_flow_record = Account_flow_DAO::getLastRecordByRestauranId($restaurant_id);
            $last_account_left = 0;
            if ($last_account_flow_record) {
                $last_account_left = $last_account_flow_record->getACCOUNT_LEFT();
            }


            $account_flow_record = new Account_flow(array());
            $account_flow_record->setACCOUNT_LEFT($last_account_left+$order_form_record->getTOTAL_PRICE_LEFT());
            $account_flow_record->setRESTAURANT_ID($restaurant_id);
            $account_flow_record->setORDER_FORM_ID($order_form_record->getORDER_FORM_ID());
            $account_flow_record->setFLOW_AMOUNT($order_form_record->getTOTAL_PRICE_LEFT());
            Account_flow_DAO::insert($account_flow_record);

            $order_form_record->setIS_ENTRY(STATE_YES);
            Order_form_DAO::update($order_form_record);

            $going_entry_record = Gonging_entry_order_form_DAO::getRecord($order_form_id);
            Gonging_entry_order_form_DAO::delete($going_entry_record);
        }
        return;
    }
}